package com.jason.ladder.webapp.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.jason.ladder.Constants;
import com.jason.ladder.model.User;
import com.jason.ladder.service.GameManager;

@Controller
public class ReportController {
	@Autowired
	GameManager gameManager;

	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView doGet(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("report");
		Calendar c1 = Calendar.getInstance();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-d");
		
		Date startDate = new Date();
		Date endDate = new Date();
		c1.setTime(startDate);
		c1.add(Calendar.WEEK_OF_YEAR, -1);
		//c1.set(field, value)
		endDate = c1.getTime();
		
		try {
			startDate = df.parse(request.getParameter("startDate"));
			endDate = df.parse(request.getParameter("endDate"));
			
		} catch (Exception e) {
			
			//e.printStackTrace();
		}
		mav.addObject("gameList", gameManager.getGamesForTimeFrame((User)request.getSession().getAttribute(Constants.SESSION_USER), startDate, endDate));
		
		return mav;
	}
}
